﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Planer.Model;

namespace Planer.Logic
{
    public class MixedTransactionListSorter
    {
        public static List<object> Sort(List<action> transactions, List<periodical_action_transanction> periodicalTransactions)
        {
            List<object> result = new List<object>();
            result.AddRange(transactions.ToArray());
            result.AddRange(periodicalTransactions.ToArray());

            result.Sort(CompareTransactionsByDate);
            return result;
        }

        public static List<object> Sort(List<allocation> allocations, List<periodical_action> periodicalActions)
        {
            List<object> result = new List<object>();
            result.AddRange(allocations.ToArray());
            result.AddRange(periodicalActions.ToArray());

            result.Sort(ComparePeriodicalActionsByDate);
            return result;
        }

        private static int ComparePeriodicalActionsByDate(object x, object y)
        {
            if (x is allocation && y is allocation)
            {
                allocation X = (allocation)x;
                allocation Y = (allocation)y;
                if (Y.create_date > X.create_date) return 1;
                if (Y.create_date < X.create_date) return -1;
                if (Y.create_date == X.create_date) return 0;
            }
            if (x is allocation && y is periodical_action)
            {
                allocation X = (allocation)x;
                periodical_action Y = (periodical_action)y;
                if (Y.create_date > X.create_date) return 1;
                if (Y.create_date < X.create_date) return -1;
                if (Y.create_date == X.create_date) return 0;
            }
            if (x is periodical_action && y is allocation)
            {
                periodical_action X = (periodical_action)x;
                action Y = (action)y;
                if (Y.create_date > X.create_date) return 1;
                if (Y.create_date < X.create_date) return -1;
                if (Y.create_date == X.create_date) return 0;
            }
            if (x is periodical_action && y is periodical_action)
            {
                periodical_action X = (periodical_action)x;
                periodical_action Y = (periodical_action)y;
                if (Y.create_date > X.create_date) return 1;
                if (Y.create_date < X.create_date) return -1;
                if (Y.create_date == X.create_date) return 0;
            }
            return 0;
        }

        public static List<object> Sort(
            List<action> transactions, 
            List<periodical_action_transanction> periodicalTransactions, 
            List<allocation_transanction> allocationTransactions)
        {
            List<object> result = new List<object>();
            result.AddRange(transactions.ToArray());
            result.AddRange(periodicalTransactions.ToArray());
            result.AddRange(allocationTransactions.ToArray());
            result.Sort(CompareTransactionsByDate);
            return result;
        }

        public static int CompareTransactionsByDate(object x, object y)
        {
            if (x is action && y is action)
            {
                action X = (action)x;
                action Y = (action)y;
                if (Y.create_date > X.create_date) return 1;
                if (Y.create_date < X.create_date) return -1;
                if (Y.create_date == X.create_date) return 0;
            }
            if (x is action && y is periodical_action_transanction)
            {
                action X = (action)x;
                periodical_action_transanction Y = (periodical_action_transanction)y;
                if (Y.create_date > X.create_date) return 1;
                if (Y.create_date < X.create_date) return -1;
                if (Y.create_date == X.create_date) return 0;
            }
            if (x is action && y is allocation_transanction)
            {
                action X = (action)x;
                allocation_transanction Y = (allocation_transanction)y;
                if (Y.create_date > X.create_date) return 1;
                if (Y.create_date < X.create_date) return -1;
                if (Y.create_date == X.create_date) return 0;
            }
            if (x is periodical_action_transanction && y is action)
            {
                periodical_action_transanction X = (periodical_action_transanction)x;
                action Y = (action)y;
                if (Y.create_date > X.create_date) return 1;
                if (Y.create_date < X.create_date) return -1;
                if (Y.create_date == X.create_date) return 0;
            }
            if (x is periodical_action_transanction && y is periodical_action_transanction)
            {
                periodical_action_transanction X = (periodical_action_transanction)x;
                periodical_action_transanction Y = (periodical_action_transanction)y;
                if (Y.create_date > X.create_date) return 1;
                if (Y.create_date < X.create_date) return -1;
                if (Y.create_date == X.create_date) return 0;
            }
            if (x is periodical_action_transanction && y is allocation_transanction)
            {
                periodical_action_transanction X = (periodical_action_transanction)x;
                allocation_transanction Y = (allocation_transanction)y;
                if (Y.create_date > X.create_date) return 1;
                if (Y.create_date < X.create_date) return -1;
                if (Y.create_date == X.create_date) return 0;
            }
            if (x is allocation_transanction && y is action)
            {
                allocation_transanction X = (allocation_transanction)x;
                action Y = (action)y;
                if (Y.create_date > X.create_date) return 1;
                if (Y.create_date < X.create_date) return -1;
                if (Y.create_date == X.create_date) return 0;
            }
            if (x is allocation_transanction && y is periodical_action_transanction)
            {
                allocation_transanction X = (allocation_transanction)x;
                periodical_action_transanction Y = (periodical_action_transanction)y;
                if (Y.create_date > X.create_date) return 1;
                if (Y.create_date < X.create_date) return -1;
                if (Y.create_date == X.create_date) return 0;
            }
            if (x is allocation_transanction && y is allocation_transanction)
            {
                allocation_transanction X = (allocation_transanction)x;
                allocation_transanction Y = (allocation_transanction)y;
                if (Y.create_date > X.create_date) return 1;
                if (Y.create_date < X.create_date) return -1;
                if (Y.create_date == X.create_date) return 0;
            }
            return 0;
        }
    }
}
